package edu.hrbeu.crc.controller;


import edu.hrbeu.crc.pojo.dto.ShoppingItemDTO;
import edu.hrbeu.crc.pojo.result.Result;
import edu.hrbeu.crc.pojo.vo.ShoppingItemVO;
import edu.hrbeu.crc.service.ShoppingItemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/shoppingList/item")
public class ShoppingItemController {

    private final ShoppingItemService shoppingItemService;

    /**
     * 添加商品
     * @param shoppingItemDTO
     * @return
     */
    @PostMapping("/addItem")
    public Result addItem(@RequestBody ShoppingItemDTO shoppingItemDTO) {
        log.info("添加商品：item为{}", shoppingItemDTO);

        shoppingItemService.add(shoppingItemDTO);
        return Result.success();
    }

    /**
     * 标记商品是否购买
     * @param itemId
     * @param status
     * @return
     */
    @PostMapping("/{itemId}")
    public Result updateStatus(@PathVariable Integer itemId, @RequestParam Integer status) {
        log.info("标记商品是否购买");

        shoppingItemService.updateStatus(itemId,status);
        return Result.success();
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{itemId}")
    public Result<ShoppingItemVO> getItem(@PathVariable(value = "itemId") Integer id) {
        log.info("根据id查询商品：{}",id);

        ShoppingItemVO vo = shoppingItemService.getItem(id);
        return Result.success(vo);
    }

    /**
     * 修改商品信息
     * @param shoppingItemDTO
     * @return
     */
    @PutMapping
    public Result updateItem(@RequestBody ShoppingItemDTO shoppingItemDTO) {
        log.info("修改商品信息：{}", shoppingItemDTO);

        shoppingItemService.updateItem(shoppingItemDTO);
        return Result.success();
    }

    /**
     * 批量删除商品
     * @param itemIds
     * @return
     */
    @DeleteMapping
    public Result deleteItem(@RequestParam(value = "itemIds") List<Integer> itemIds) {
        log.info("批量删除清单商品：{}",itemIds);

        shoppingItemService.delete(itemIds);
        return Result.success();
    }
}
